Disable second precision schedule by default to prevent service abuse.

Akinori MUSHA 10 years ago
parent
commit
0f66669346
3 changed files with 35 additions and 10 deletions
  1. 6 0
      .env.example
  2. 18 7
      app/models/agents/scheduler_agent.rb
  3. 11 3
      spec/models/agents/scheduler_agent_spec.rb

+ 6 - 0
.env.example

@@ -104,6 +104,12 @@ ALLOW_JSONPATH_EVAL=false
104 104
 # when you trust everyone using your Huginn installation.
105 105
 ENABLE_INSECURE_AGENTS=false
106 106
 
107
+# Enable this setting to allow second precision schedule in
108
+# SchedulerAgent.  By default, the use of the "second" field is
109
+# restricted so that any value other than a single zero (which means
110
+# "on the minute") is disallowed.
111
+ENABLE_SECOND_PRECISION_SCHEDULE=false
112
+
107 113
 # Use Graphviz for generating diagrams instead of using Google Chart
108 114
 # Tools.  Specify a dot(1) command path built with SVG support
109 115
 # enabled.

+ 18 - 7
app/models/agents/scheduler_agent.rb

@@ -8,7 +8,11 @@ module Agents
8 8
     cannot_receive_events!
9 9
     cannot_create_events!
10 10
 
11
-    description <<-MD
11
+    @@second_precision_enabled = ENV['ENABLE_SECOND_PRECISION_SCHEDULE'] == 'true'
12
+
13
+    cattr_reader :second_precision_enabled
14
+
15
+    description <<-MD % { seconds: (<<-MD_SECONDS if second_precision_enabled) }
12 16
       This agent periodically triggers a run of each target Agent according to a user-defined schedule.
13 17
 
14 18
       # Targets
@@ -34,11 +38,7 @@ module Agents
34 38
 
35 39
       * `0 22 * * 1-5 Etc/GMT+2`: every day of the week when it's 22:00 in GMT+2
36 40
 
37
-      ## Seconds
38
-
39
-      You can optionally specify seconds before the minute field.
40
-
41
-      * `*/30 * * * * *`: every 30 seconds
41
+      %{seconds}
42 42
 
43 43
       ## Last day of month
44 44
 
@@ -61,6 +61,14 @@ module Agents
61 61
       * `0 22 * * Sun#L1`: every last Sunday of the month, at 22:00
62 62
     MD
63 63
 
64
+      ## Seconds
65
+
66
+      You can optionally specify seconds before the minute field.
67
+
68
+      * `*/30 * * * * *`: every 30 seconds
69
+
70
+    MD_SECONDS
71
+
64 72
     def default_options
65 73
       super.update({
66 74
         'schedule' => '0 * * * *',
@@ -78,7 +86,10 @@ module Agents
78 86
     def validate_options
79 87
       if (spec = options['schedule']).present?
80 88
         begin
81
-          Rufus::Scheduler::CronLine.new(spec)
89
+          cron = Rufus::Scheduler::CronLine.new(spec)
90
+          if !second_precision_enabled && cron.seconds != [0]
91
+            errors.add(:base, "second precision schedule is not allowed in this service")
92
+          end
82 93
         rescue ArgumentError
83 94
           errors.add(:base, "invalid schedule")
84 95
         end

+ 11 - 3
spec/models/agents/scheduler_agent_spec.rb

@@ -26,14 +26,22 @@ describe Agents::SchedulerAgent do
26 26
       @agent.options['schedule'] = '*/15 * * * * * *'
27 27
       @agent.should_not be_valid
28 28
 
29
-      @agent.options['schedule'] = '*/15 * * * * *'
30
-      @agent.should be_valid
31
-
32 29
       @agent.options['schedule'] = '*/1 * * * *'
33 30
       @agent.should be_valid
34 31
 
35 32
       @agent.options['schedule'] = '*/1 * * *'
36 33
       @agent.should_not be_valid
34
+
35
+      stub(@agent).second_precision_enabled { true }
36
+      @agent.options['schedule'] = '*/15 * * * * *'
37
+      @agent.should be_valid
38
+
39
+      stub(@agent).second_precision_enabled { false }
40
+      @agent.options['schedule'] = '*/15 * * * * *'
41
+      @agent.should_not be_valid
42
+
43
+      @agent.options['schedule'] = '0 * * * * *'
44
+      @agent.should be_valid
37 45
     end
38 46
   end
39 47